package com.hnyyjsxy.web.servlet;

import com.hnyyjsxy.domain.ResultInfo;
import com.hnyyjsxy.domain.Teacher;
import com.hnyyjsxy.service.SuperAdmainService;
import com.hnyyjsxy.service.TeacherService;
import com.hnyyjsxy.service.impl.SuperAdmainServiceImpl;
import com.hnyyjsxy.service.impl.TeacherServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @date 2021/02/07
 */
@WebServlet("/super_admain_html/superAdmain/*")
public class SuperAdmainServlet extends BeanServlet {
    //创建业务层对象
    TeacherService teacherService = new TeacherServiceImpl();
    SuperAdmainService superAdmainService = new SuperAdmainServiceImpl();

    /**
     * 添加宿管信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void addTeacher(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 使用BeanUtils获取请求参数
        Map<String, String[]> parameterMap = request.getParameterMap();
        Teacher teacher = new Teacher();
        try {
            BeanUtils.populate(teacher,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //2. 获取两次密码 判断密码是否正确
        String password = request.getParameter("password");
        String password1 = request.getParameter("password1");
        //创建ResultInfo对象
        ResultInfo resultInfo = new ResultInfo();
        if (password!= null && password.equals(password1)){
            //3. 判断该账号是否存在
            Teacher tea = teacherService.getTeacherByAccount(teacher.getAccount());
            if (tea == null){
                //4. 将teacher对象传给业务层
                superAdmainService.addTeacher(teacher,password);
                resultInfo.setFlag(true);
            }else{
                resultInfo.setFlag(false);
                resultInfo.setErrorMsg("该账号已存在！");
            }
        }else{
            resultInfo.setFlag(false);
            resultInfo.setErrorMsg("两次密码输入不一致！");
        }

        //5. 回响
        respondOnly(resultInfo,response);
    }

    /**
     * 查询宿管信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void queryTeacher(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 调用业务层查询宿管老师信息，返回值的一个list集合
        List<Teacher> teachers = superAdmainService.queryAllTeacher();
        //2. 响应
        respondOnly(teachers,response);
    }

    /**
     * 删除宿管老师信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void delTeacher(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 获取请求参数
        String account = request.getParameter("account");
        //2. 调用业务层，参数是宿管老师账号
        superAdmainService.delTeacherByAccount(account);
        //3. 重定向至查询宿管老师信息的界面
        response.sendRedirect(request.getContextPath()+"/super_admain_html/admain_main_page.html");
    }


}
